package com.tianji.remark.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.remark.domain.dto.LikeRecordFormDTO;
import com.tianji.remark.domain.po.LikedRecord;
import com.tianji.remark.mapper.LikedRecordMapper;
import com.tianji.remark.service.ILikedRecordService;
import com.tianji.remark.service.ILikedStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-26
 */
//@Service
public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {

    @Autowired
    private RabbitMqHelper mqHelper;
    @Autowired
    private ILikedStatService likedStatService;

    /**
     * 添加点赞记录
     *
     * @param dto
     */

    @Override
    @Transactional
    public void addLike(LikeRecordFormDTO dto) {
        //获取当前用户id
        Long userId = UserContext.getUser();
        if (userId == null) {
            throw new RuntimeException("用户未登录");
        }
        if (dto.getLiked()) {
            like(userId, dto);
        } else {
            unLike(userId, dto);
        }

//        Boolean liked = dto.getLiked() ? like(userId, dto) : unLike(userId, dto);
//
//        if (!liked){
//            throw new CommonException("点赞或取消点赞失败");
//        }

        //查询点赞数并发送消息
        Integer count = this.lambdaQuery()
                .eq(LikedRecord::getBizId, dto.getBizId())
                .count();
        LikedTimesDTO likedTimesDTO = new LikedTimesDTO();
        likedTimesDTO.setBizId(dto.getBizId());
        likedTimesDTO.setLikedTimes(count);
        String exchange = MqConstants.Exchange.LIKE_RECORD_EXCHANGE;
        String key = StringUtils.format(MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE, dto.getBizType());
        mqHelper.send(exchange, key, likedTimesDTO);
    }

    /**
     * 取消点赞
     *
     * @param userId
     * @param dto
     * @return
     */
    private Boolean unLike(Long userId, LikeRecordFormDTO dto) {
        //判断是否点过赞
        if (!isLiked(userId, dto)) {
            return false;
        }
        return this.lambdaUpdate().eq(LikedRecord::getUserId, userId)
                .eq(LikedRecord::getBizId, dto.getBizId())
                .eq(LikedRecord::getBizType, dto.getBizType())
                .remove();
    }

    /**
     * 点赞
     *
     * @param userId
     * @param dto
     * @return
     */
    private Boolean like(Long userId, LikeRecordFormDTO dto) {
        //判断是否点过赞
        if (!isLiked(userId, dto)) {
            return false;
        }
        //保存点赞记录
        LikedRecord record = new LikedRecord();
        record.setUserId(userId);
        record.setBizId(dto.getBizId());
        record.setBizType(dto.getBizType());
        return this.save(record);
    }

    /**
     * 判断当前用户是否点过赞
     *
     * @param userId
     * @param dto
     * @return
     */
    private boolean isLiked(Long userId, LikeRecordFormDTO dto) {
        Integer count = this.lambdaQuery().eq(LikedRecord::getUserId, userId)
                .eq(LikedRecord::getBizId, dto.getBizId())
                .eq(LikedRecord::getBizType, dto.getBizType())
                .count();
        return count > 0;

    }

    /**
     * 查询点赞列表
     *
     * @param bizIds
     * @return
     */
    @Override
    public Set<Long> isBizLiked(List<Long> bizIds) {
        //获取当前用户id
        Long userId = UserContext.getUser();
        List<LikedRecord> list = this.lambdaQuery()
                .eq(LikedRecord::getUserId, userId)
                .in(LikedRecord::getBizId, bizIds)
                .list();
        return list.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
    }
}
